// pages/discount_square/discount_square.js
var common = require("../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        discount: []
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        wx.hideShareMenu();
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        this.getData();
        this.getBase();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    getData: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Voucher/voucher/list",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                console.log(res);
                that.setData({
                    discount: res.extra
                });
            }
        });
    },
    receive: function(e) {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        var id = e.currentTarget.dataset.id;
        common.request({
            endpoint: "Voucher/voucher/draw",
            method: "post",
            data: {
                openid: openid,
                vor_id: id
            },
            success: res => {
                wx.showToast({
                    title: res.message,
                    success: res => {
                        setTimeout(() => {
                            that.onLoad();
                        }, 500);
                    }
                });
            }
        });
    },
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            data: {},
            success: res => {
                wx.setNavigationBarTitle({
                    title: res.extra.rp_name + "广场"
                });
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                that.setData({
                    rp_name: res.extra.rp_name
                });
            }
        });
    }
});